﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Security.Cryptography;

namespace SES.CMS.AdminCP
{
    public partial class Album : System.Web.UI.Page
    {
        cmsAlbumDO objAlbum = new cmsAlbumDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["AlbumID"] != null)
            {
                objAlbum.AlbumID = int.Parse(Request.QueryString["AlbumID"].ToString());
                if (!IsPostBack)
                    initForm();
            }
        }
        private void initForm()
        {
            objAlbum = new cmsAlbumBL().Select(objAlbum);
            txtTitle.Text = objAlbum.Title;
            txtDesc.Text = objAlbum.Description;
            if (objAlbum.Type == true)
                rdAlbum.Checked = true;
            else
                rdVideo.Checked = true;
            txtOrderID.Text = objAlbum.OrderID.ToString();
            if (!string.IsNullOrEmpty(objAlbum.AnhAlbum))
            {
                hplImage.Visible = true;
                hplImage.NavigateUrl = "/Media/" + objAlbum.AnhAlbum;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objAlbum.AlbumID <= 0)
            {
                new cmsAlbumBL().Insert(objAlbum);
            }
            else
            {
                new cmsAlbumBL().Update(objAlbum);
            }
            Functions.Alert("Cập nhật thành công!", "/AdminCP/ListAlbum.aspx");
        }
        private void initObject()
        {
            objAlbum = new cmsAlbumBL().Select(objAlbum);
            objAlbum.Title = txtTitle.Text;
            if (fuImage1.HasFile)
                objAlbum.AnhAlbum = UploadFile(fuImage1);
            objAlbum.AnhKieu = "";
            objAlbum.Description = txtDesc.Text;
            objAlbum.OrderID = int.Parse(txtOrderID.Text.Trim());
            if (rdAlbum.Checked)
                objAlbum.Type = true;
            else
                objAlbum.Type = false;
            objAlbum.CategoryID = 0;
        }
        private string UploadFile(FileUpload fulAlbum)
        {
            if (!string.IsNullOrEmpty(fulAlbum.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulAlbum.FileName.Substring(fulAlbum.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulAlbum.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
       
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminCP/ListAlbum.aspx");
        }
    }
}